package com.lcxw.hxzb.friendcircle.project.system.controller;

import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;
import com.lcxw.hxzb.friendcircle.project.system.domain.*;
import com.lcxw.hxzb.friendcircle.project.system.service.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 游戏信息Controller
 *
 * @author ruoyi
 * @date 2020-09-15
 */
@RestController
@Api(tags = "游戏信息Controller")
@RequestMapping("/system/game/info")
public class YwpdManageGameInfoController extends BaseController {
    @Autowired
    private IYwpdGameInfoService ywpdGameInfoService;
    @Autowired
    private IYwpdGamelevelService ywpdGamelevelService;
    @Autowired
    private IYwpdGameService ywpdGameService;
    @Autowired
    private IYwpdGameDistrictService ywpdGameDistrictService;
    @Autowired
    private IYwpdDistrictService ywpdDistrictService;
    @Autowired
    private IYwpdInfoDistrictService ywpdInfoDistrictService;
    @Autowired
    private IYwpdGameOrderService ywpdGameOrderService;
    @Autowired
    private GiftService giftService;


    /**
     * 查询陪玩游戏列表
     */
    @GetMapping("/list")
    public TableDataInfo list(YwpdGameInfo ywpdGameInfo) {
        startPage();
        List<YwpdGameInfo> gameInfoList = ywpdGameInfoService.selectYwpdGameInfoList(ywpdGameInfo);
        try {
            Map<Long, List<YwpdGamelevel>> levelList = ywpdGamelevelService.selectYwpdGamelevelList(null).stream().collect(Collectors.groupingBy(YwpdGamelevel::getLevelId));
            Map<Long, List<YwpdGame>> gameList = ywpdGameService.selectYwpdGameList(null).stream().collect(Collectors.groupingBy(YwpdGame::getGameId));
            //Map<Long, List<YwpdGameDistrict>> gameDistrictList = ywpdGameDistrictService.selectYwpdGameDistrictList(null).stream().collect(Collectors.groupingBy(YwpdGameDistrict::getDistrictId));
            //查找所有礼物
            Map<Integer, List<Gift>> gifts = giftService.selectAllEnableGift().stream().collect(Collectors.groupingBy(Gift::getId));
            Map<Long, List<YwpdDistrict>> districtList = ywpdDistrictService.selectYwpdDistrictList(null).stream().collect(Collectors.groupingBy(YwpdDistrict::getDistrictId));
            Map<Long, List<YwpdInfoDistrict>> infoDistrictList = ywpdInfoDistrictService.selectYwpdInfoDistrictList(null).stream().collect(Collectors.groupingBy(YwpdInfoDistrict::getInfoId));
            Map<Long, List<YwpdGameOrder>> gameOrderList = ywpdGameOrderService.selectYwpdGameOrderList(null).stream().collect(Collectors.groupingBy(YwpdGameOrder::getInfoId));
            //遍历 分别把数据塞到gameInfo
            for (YwpdGameInfo gameInfo : gameInfoList) {
                //礼物
                Integer giftId = Math.toIntExact(gameInfo.getGiftId());
                List<Gift> giftList = gifts.get(giftId);
                gameInfo.setGift(giftList.get(0));
                //陪玩游戏
                Long gameId = gameInfo.getGameId();
                YwpdGame game = gameList.get(gameId).get(0);
                gameInfo.setYwpdGame(game);
                //游戏区服
                Long infoId = gameInfo.getInfoId();
                List<YwpdInfoDistrict> ywpdInfoDistricts = infoDistrictList.get(infoId);
                List<YwpdDistrict> districts = new LinkedList<>();
                for (YwpdInfoDistrict districtL : ywpdInfoDistricts) {
                    Long districtId = districtL.getDistrictId();
                    YwpdDistrict district = districtList.get(districtId).get(0);
                    districts.add(district);
                }
                gameInfo.setYwpdDistrict(districts);
            }
        } catch (Exception e) {
            System.out.println("拉取数据异常，请重试！:" + e);
        }
        return getDataTable(gameInfoList);
    }



    /**
     * 修改游戏信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdGameInfo ywpdGameInfo) {
        return toAjax(ywpdGameInfoService.updateYwpdGameInfo(ywpdGameInfo));
    }

}
